$(document).ready(function(){

	// Case: editing prorole's company
	$('input[name=studentactivity-company]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_companies.php',
			data:	'id='+$('input[name=studentactivity-id]').val(),
			success: function(msg) {
				var select = $('div#select_studentactivity');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:checkbox:checked');
							if (checked.length <= 0)
								$('input[name=studentactivity-company]').val('');
							else
								$('input[name=studentactivity-company]').val(checked.first().val());
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: editing prorole's contact
	$('input[name=studentactivity-contact]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_contacts.php',
			data:	'id='+$('input[name=studentactivity-id]').val(),
			success: function(msg) {
				var select = $('div#select_contact');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:checkbox:checked');
							if (checked.length <= 0)
								$('input[name=studentactivity-contact]').val('');
							else
								$('input[name=studentactivity-contact]').val(checked.first().val());
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
});
